package com.livery.app.view.frame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import org.hibernate.Query;
import org.hibernate.Session;

import com.livery.app.controller.AbstractWorkspaceController;
import com.livery.app.main.HibernateUtil;
import com.livery.app.model.observators.ClientObserver;
import com.livery.app.model.tables.ClientModel;
import com.livery.data.Client;

public class ClientFrame extends JFrame implements ClientObserver, ActionListener{

	private AbstractWorkspaceController controller;
	private List<Client> list = new ArrayList<Client>();
	private JTable tableClient;
	private TableModel model;
	private JButton btnSearch;
	private JButton btnAddClient;
	private JButton btnEditClient;
	
	private JTextField fieldName;
	private JTextField fieldSurname;
	private JTextField fieldID;
	
	public ClientFrame(AbstractWorkspaceController controller){
		super("Klienci");
		this.controller = controller;
		this.setMinimumSize(new Dimension(800,600));
		this.addWindowListener	(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e){ 
				setVisible(false);				
			}	
		});	

		Dimension dialogSize = getSize();		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		if(dialogSize.height > screenSize.height) 
			dialogSize.height = screenSize.height;
		if(dialogSize.width > screenSize.width)
			dialogSize.height = screenSize.width;
		setLocation((screenSize.width-dialogSize.width)/2,   
						(screenSize.height-dialogSize.height)/2);
		createView();
	}

	private void createView(){
		this.setLayout(new BorderLayout());
		this.add(createNorthPanel(), BorderLayout.NORTH);
		this.add(createCenterPanel(), BorderLayout.CENTER);
		this.add(createSouthPanel(), BorderLayout.SOUTH);
		
	}
	private JPanel createNorthPanel(){
		JPanel panel = new JPanel(new FlowLayout());
		JLabel labelName = new JLabel("Imię:", SwingConstants.RIGHT);
		JLabel labelSurname = new JLabel("Nazwisko:", SwingConstants.RIGHT);
		JLabel labelID = new JLabel("Nr dowodu:", SwingConstants.RIGHT);
		fieldName = new JTextField(10);
		fieldSurname = new JTextField(10);
		fieldID = new JTextField(10);
		btnSearch = createButton("Szukaj", "Wyszukaj klientów");
		panel.add(labelName);
		panel.add(fieldName);
		panel.add(labelSurname);
		panel.add(fieldSurname);
		panel.add(labelID);
		panel.add(fieldID);
		panel.add(btnSearch);
		return panel;
	}
	
	private JPanel createSouthPanel(){
		JPanel panel = new JPanel(new FlowLayout());
		btnAddClient = createButton("Dodaj", "Dodaj nowego klienta");
		btnEditClient = createButton("Edytuj", "Edytuj aktualnie zaznaczonego klienta");
		panel.add(btnAddClient);
		panel.add(btnEditClient);
		return panel;
	}
	private JPanel createCenterPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		//controller.downloadClientList(0, 0);
		model = new ClientModel(list);
		tableClient = new JTable(model);
		tableClient.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    ListSelectionModel selectionModel = tableClient.getSelectionModel();
		selectionModel.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// TODO Auto-generated method stub
				setEnabledButtonEdit();
			}
		});
		
		JScrollPane scrollPane = new JScrollPane(tableClient);
		panel.add(scrollPane);
		return panel;
	}
	public void setDisabledButtonEdit(){
		setClearSelection();
		btnEditClient.setEnabled(false);
	}
	private void setClearSelection(){
		tableClient.clearSelection();
	}
	private void setEnabledButtonEdit(){
		btnEditClient.setEnabled(true);
	}
	private JButton createButton(String text, String toolTip){
		JButton button = new JButton(text);
		button.setToolTipText(toolTip);
		button.addActionListener(this);
		button.setEnabled(true);
		return button;
	}
	@Override
	public void updateClient(List<Client> list) {
		// TODO Auto-generated method stub
		this.list =  list;
		((ClientModel)model).setListClient(list);
		tableClient.revalidate();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == btnSearch){
			String name = fieldName.getText();
			String surname = fieldSurname.getText();
			String documentID = fieldID.getText();
			controller.searchClient(name, surname, documentID);
		}
		if(e.getSource() == btnAddClient){
			controller.addClient();
		}
		if(e.getSource() == btnEditClient){
			int row = tableClient.getSelectedRow();
			Client editClient = ((ClientModel)model).getClientAt(row);
			for(int i = 0; i < model.getColumnCount(); i++){
				System.out.print(model.getValueAt(row, i) + "     ");
			}
			System.out.println();
			controller.updateClient(editClient);
		}
	}
}
